package com.example.elderlycare.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.example.elderlycare.controller.entity.ConsultRequestBody;
import com.example.elderlycare.dao.entity.ConsultRecord;
import com.example.elderlycare.service.ConsultService;
import com.example.elderlycare.service.entity.ConsultRecordListResponseBody;
import com.example.elderlycare.utils.Result;
import com.example.elderlycare.utils.ResultEnum;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.BeanUtils;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.validation.Valid;
import javax.validation.constraints.NotEmpty;
import java.util.Date;

@RestController
@RequestMapping("/consult")
public class ConsultRecordController {
    private final Logger logger = LoggerFactory.getLogger(EquipmentController.class);

    @Resource
    ConsultService consultService;

    /**
     * queryConsultRecordList 根据参数获取咨询登记列表
     *
     * @param consult  咨询人
     * @param content  内容
     * @param phone    联系方式
     * @param pageSize 页面通量
     * @param current  当前页码
     * @return ConsultRecordListResponseBody
     */
    @GetMapping
    public Result<ConsultRecordListResponseBody> queryConsultRecordList(
            @RequestParam(value = "consult", required = false) String consult,
            @RequestParam(value = "phone", required = false) String phone,
            @RequestParam(value = "content", required = false) String content,
            @RequestParam(defaultValue = "10", value = "pageSize") int pageSize,
            @RequestParam(defaultValue = "1", value = "current") int current
    ) {
        QueryWrapper<ConsultRecord> queryWrapper = new QueryWrapper<>();
        queryWrapper.like(consult != null && !"".equals(consult), "name", consult)
                .like(phone != null && !"".equals(phone), "phone", phone)
                .like(content != null && !"".equals(content), "content", content);
        ConsultRecordListResponseBody responseBody = consultService.queryConsultRecordList(current, pageSize, queryWrapper);
        if (responseBody == null) {
            Result.error(ResultEnum.DEFAULT_ERROR);
        }
        return Result.success(responseBody);
    }

    /**
     * addConsultRecord 添加咨询登记记录
     *
     * @param req ConsultRequestBody.AddConsultRequestBody
     * @return null
     */
    @PostMapping
    public Result<?> addConsultRecord(@Valid @RequestBody ConsultRequestBody.AddConsultRequestBody req) {
        ConsultRecord consultRecord = new ConsultRecord();
        BeanUtils.copyProperties(req, consultRecord);
        consultRecord.setDate(new Date());
        try {
            boolean success = consultService.save(consultRecord);
            if (success) {
                return Result.success(null);
            } else {
                logger.warn("add consultRecord error");
                return Result.error(ResultEnum.DEFAULT_ERROR);
            }
        } catch (Exception e) {
            logger.warn("Exception", e);
            return Result.error(ResultEnum.DEFAULT_ERROR);
        }
    }


    /**
     * updateConsultRecord 根据id更新信息
     *
     * @param req ConsultRequestBody.UpdateConsultRequestBody
     * @return null
     */
    @PutMapping
    public Result<?> updateConsultRecord(@Valid @RequestBody ConsultRequestBody.UpdateConsultRequestBody req) {
        ConsultRecord consultRecord = new ConsultRecord();
        BeanUtils.copyProperties(req, consultRecord);
        try {
            boolean success = consultService.updateById(consultRecord);
            if (success) {
                return Result.success(null);
            } else {
                logger.warn("update consultRecord error");
                return Result.error(ResultEnum.DEFAULT_ERROR);
            }
        } catch (Exception e) {
            logger.warn("Exception", e);
            return Result.error(ResultEnum.DEFAULT_ERROR);
        }
    }

    /**
     * deleteConsultRecord 根据id删除
     *
     * @param id 需要删除记录的id
     * @return null
     */
    @DeleteMapping
    public Result<?> deleteConsultRecord(@RequestParam("id") @NotEmpty Integer id) {
        try {
            boolean success = consultService.removeById(id);
            if (success) {
                return Result.success(null);
            } else {
                logger.warn("delete consultRecord error");
                return Result.error(ResultEnum.DEFAULT_ERROR);
            }
        } catch (Exception e) {
            logger.warn("Exception", e);
            return Result.error(ResultEnum.DEFAULT_ERROR);
        }
    }
}
